package util;

import lombok.SneakyThrows;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author JoeZhou
 */
public class MyBatisUtil {

    private static SqlSessionFactory factory;

    @SneakyThrows
    public static SqlSessionFactory getFactory(String resource) {
        if (factory == null) {
            synchronized (MyBatisUtil.class) {
                if (factory == null) {
                    factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
                }
            }
        }
        return factory;
    }
}